package com.qf.smsplatform.search.mq;


import com.qf.smsplatform.common.constants.RabbitMQConstants;
import com.qf.smsplatform.common.index.StandardSubmit;
import com.qf.smsplatform.search.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class SmsLogListener {

    @Autowired
    private SearchService searchService;

    @StreamListener(RabbitMQConstants.TOPIC_SMS_SEND_LOG)
    public  void  listen(StandardSubmit submit,
                         @Header(name = AmqpHeaders.CHANNEL)Channel channel,
                         @Header(name = AmqpHeaders.DELIVERY_TAG)Long deliveryTag){

   log.info("【搜索模块】 接口到写日志的消息 submit={}",submit);

        try {
            searchService.add(submit);
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
//es连接有问题

            System.out.println("es连接有问题");
        }catch (RuntimeException e) {
            System.out.println("插入数据失败");
        }

    }
}
